package com.shou.trigger.http;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shou.api.model.dto.*;
import com.shou.api.model.response.ResultData;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.park.model.ParkingLotEntity;
import com.shou.domain.park.model.ParkingSpaceEntity;
import com.shou.domain.park.service.IParkingSpaceService;
import com.shou.trigger.http.mapstruct.ParkingSpaceDTOConvert;
import com.shou.trigger.http.mapstruct.ParkingSpaceInfoDTOConvert;
import com.shou.trigger.http.mapstruct.common.PageQueryDTOConvert;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

/**
 * ClassName:ParkingSpaceController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description:停车位管理，增删改查。
 *
 * @Author Nemo
 * @Create 2025/2/8 17:09
 * @Version 1.0
 */
@Validated
@RestController
@CrossOrigin
@RequestMapping("/api/v1/parking-space")
public class ParkingSpaceController extends AbstructController{
    @Resource
    private IParkingSpaceService parkingSpaceService;
    @Resource
    private PageQueryDTOConvert pageQueryDTOConvert;
    @Resource
    private ParkingSpaceDTOConvert parkingSpaceDTOConvert;
    @Resource
    private ParkingSpaceInfoDTOConvert parkingSpaceInfoDTOConvert;

    /**
     * 分页查询
     */
    @GetMapping("/parking-space-info")
    public ResultData<IPage<ParkingSpaceDTO>> page(@RequestParam Map<String, Object> rulePageQueryDTOMap){
        PageQueryDTO pageQueryDTO = PageQueryDTO.getRulePageQueryDTOFromMap(rulePageQueryDTOMap);
        // 入参校验
        validateQueryParams(pageQueryDTO);
        // 将DTO对象转换为实体类
        PageQueryEntity pageQueryEntity = pageQueryDTOConvert.convertPageQueryDTOToEntity(pageQueryDTO);
        // 分页查询
        IPage<ParkingSpaceEntity> queueMessageList = parkingSpaceService.pageQuery(pageQueryEntity);
        return ResultData.success(parkingSpaceDTOConvert.convertParkingSpaceEntityPageToDTO(queueMessageList));
    }
    /**
     * 获取对应编号停车场可用车位数和总车位总数
     */
    @GetMapping("/parking-space-available")
    public ResultData<ParkingSpaceInfoDTO> getSpacesAvailableInfoByCode(@RequestParam Map<String, Object> queryDTOMap){
        String lotCode = Optional.ofNullable(queryDTOMap.get("lotCode"))
                            .map(String::valueOf)
                            .orElse(null);
        ParkingSpaceInfoDTO parkingSpaceInfoDTO =parkingSpaceInfoDTOConvert.convertParkingSpaceInfoEntityToDTO(parkingSpaceService.getSpacesAvailableInfoByCode(lotCode));
        return ResultData.success(parkingSpaceInfoDTO);
    }

    /**
     * 新增车位信息
     */
    @PostMapping("/parking-space-info")
    public ResultData<String> insertParkingSpace(@RequestBody ParkingSpaceDTO parkingSpaceDTO){
        // 将DTO对象转换为Entity对象，调用domain层方法
        ParkingSpaceEntity parkingSpaceEntity = parkingSpaceDTOConvert.convertParkingSpaceDTOToEntity(parkingSpaceDTO);
        // 保存(新增)规则
        parkingSpaceService.insertParkingSpace(parkingSpaceEntity);
        return ResultData.success("insertParkingSpace success");
    }

    /**
     * 删除单/多个车位信息
     */
    @DeleteMapping("/parking-space-info")
    public ResultData<String> deleteParkingSpaceByIds(@RequestBody @Validated DeleteDTO deleteDTO) {
        // 根据传递的ID(可以是多个)删除车位信息
        parkingSpaceService.deleteByIds(deleteDTO.getIds());
        return ResultData.success("deleteParkingSpaceByIds success");
    }

    /**
     * 清空所有车位信息
     */
    @DeleteMapping("/all-parking-space-info")
    public ResultData<String> deleteAll() {
        // 清空车位信息表
        parkingSpaceService.deleteAll();
        return ResultData.success("deleteAll success");
    }

    /**
     * 根据车位ID更新车位信息
     */
    @PutMapping("/parking-space-info")
    public ResultData<String> updateParkingSpaceById(@RequestBody @Validated ParkingSpaceDTO rule) {
        // 将DTO对象转换为Entity对象，调用domain层方法
        ParkingSpaceEntity parkingSpaceEntity = parkingSpaceDTOConvert.convertParkingSpaceDTOToEntity(rule);
        // 根据ID更新车位信息
        parkingSpaceService.updateParkingSpaceById(parkingSpaceEntity);
        return ResultData.success("updateParkingSpaceById success");
    }

    /**
     * 根据车位编号和车位编号查询车位信息
     */
    @GetMapping("/parking-space-info-precise")
    public ResultData<ParkingSpaceDTO> getParkingSpaceByCode(@RequestParam Map<String, Object> queryDTOMap){
        String lotCode = Optional.ofNullable(queryDTOMap.get("lotCode"))
                .map(String::valueOf)
                .orElse(null);
        String spaceCode = Optional.ofNullable(queryDTOMap.get("spaceCode"))
                .map(String::valueOf)
                .orElse(null);
        ParkingSpaceDTO parkingSpaceDTO = parkingSpaceDTOConvert.convertParkingSpaceEntityToDTO(parkingSpaceService.getParkingSpaceByCode(lotCode, spaceCode));
        return ResultData.success(parkingSpaceDTO);
    }
}
